﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel.Configuration;
using System.Web.Configuration;
using System.ServiceModel.Activation;
using com.iss.inventory.Controllers;
using com.iss.inventory.Entity;
using com.iss.inventory.CustomModels;
using MobileService.DataModels;


namespace MobileService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
   [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
   public class Service1 : IService1
   {

       StockCardController stockCardControl = new StockCardController();
        UserMaintenanceController empControl = new UserMaintenanceController();
        ViewAdjVoucherController adjVoucherControl = new ViewAdjVoucherController();
        ApproveAdjVoucherController approveAdjVoucherControl = new ApproveAdjVoucherController();
       AddItemToVoucherController additemtoVoucherControl = new AddItemToVoucherController();
      
       public void AddItemtoVoucher(AdjVoucherDetailsMobile adjdvm)
       {
           DateTime currentDate = System.DateTime.Now;
           String feedback = additemtoVoucherControl.addAdjustmentVoucher(adjdvm.ItemID.ToString(), adjdvm.QtyAdj, adjdvm.Reason, currentDate);
       }

       //public string[] getItemBylocation(string Location)
       //{

       //    String[] newString = additemtoVoucherControl.getItemByBinNo(Location);
       //    return newString;
       //}

       //public string[] getItemByItemID(string ItemID)
       //{

       //    String[] newString = additemtoVoucherControl.getItemByItemCode(ItemID);
       //    return newString;
       //}

       public StationeryItemMobile getItemBylocation(string Location)
       {

           String[] newString = additemtoVoucherControl.getItemByBinNo(Location);
           return StationeryItemMobile.Make(Convert.ToInt32(newString[1]), "0", newString[2], Convert.ToInt32(newString[3]), 0, 0, "0", newString[0]);
       }

       public StationeryItemMobile getItemByItemID(string ItemID)
       {

           String[] newString = additemtoVoucherControl.getItemByItemCode(ItemID);
           return StationeryItemMobile.Make(Convert.ToInt32(newString[1]), "0", newString[2], Convert.ToInt32(newString[3]), 0, 0, "0", newString[0]);
       }
          
       public string[] empDetails(string id)
       {
           //return new string[]{"1","
           return empControl.getEmpByID(id);
       }

       public AdjVoucherDetailsMobile GetAdjVoucherDetail(string avdid)
       {
           InvManagementEntities em = new InvManagementEntities();
           int id = Convert.ToInt32(avdid);
           AdjVoucherDetail temp = em.AdjVoucherDetails.Where(x => x.AdjVoucherDetailsID == id).FirstOrDefault();
           VoucherDetailsModel model = new VoucherDetailsModel();
           model.AdjVoucherDetailID = temp.AdjVoucherDetailsID;
           model.ItemID=Convert.ToInt32(temp.ItemID);
           model.QtyAdj=temp.QtyAdjust.ToString();
           model.Reason=temp.Reason;
           model.Cost=Convert.ToDouble(temp.QtyAdjust)*Convert.ToDouble(temp.StationeryItem.SupplierPriceLists.Where(x=>x.ItemID==temp.ItemID).FirstOrDefault().SupplierPrice);
           if (model.Cost < 0)
               model.Cost = model.Cost * (-1);
           return AdjVoucherDetailsMobile.convert(model);
       }
     
        //string GetData(int vale);
       //public AdjVoucherMobile GetAdjVoucherDetails(string adjustid)
       //{
       //    List<VoucherDetailsModel> voucherDetailsList = adjVoucherControl.getVoucherByAdjustID(adjustid);
       //    AdjVoucher adjVoucherControl = adjVoucherControl.getAdjVoucher(adjustid);
       //    return AdjVoucherMobile.convert(adjVoucher, voucherDetailsList);
       //}

       public AdjVoucherDetailsMobile[] GetPendingAdjVoucherDetails(string role)
       {
           List<VoucherDetailsModel> voucherDetailsList = approveAdjVoucherControl.getAllPendingVouchers(role);
           List<AdjVoucherDetailsMobile> adjVoucherDetailsListMobile = AdjVoucherDetailsMobile.convertList(voucherDetailsList);
           return adjVoucherDetailsListMobile.ToArray<AdjVoucherDetailsMobile>();
       }

       public string DeleteAdjVoucherDetails(int adjvdid)
       {
           string feedback = approveAdjVoucherControl.deletePendingVoucher(adjvdid);
           return feedback;
       }

       public string ApproveAdjVoucherDetails(string empid, List<int> voucherList)
       {
           DateTime currentTime = System.DateTime.Now;
           string feedback = approveAdjVoucherControl.approvePendingVoucher(voucherList, empid, currentTime);
           return feedback;
       }

       public string GetData()
       {
           return string.Format("You entered: {0}", "KMB");
       }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
               throw new ArgumentNullException("composite");
           }
           if (composite.BoolValue)
           {
               composite.StringValue += "Suffix";
           }
            return composite;
        }

        public StockCardMobile GetStockCardDetails(string itemid)
        {
            StockCardModel stockCard = stockCardControl.getStockCardbyItemId(itemid);
            StockCardModel newCard = new StockCardModel();
            newCard.setYear(stockCard.getYear());
            newCard.setMonth(stockCard.getMonth());
            StationeryItem newItem = new StationeryItem();
            
            StockCardMobile stockCardMobile = StockCardMobile.convert(stockCard);
            return stockCardMobile;
        }
    }
}
